<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px">
            <el-form-item label="排序" prop="sort">
                <el-input v-model="ruleForm.sort"></el-input>
            </el-form-item>
            <el-form-item label="菜单名称" prop="title">
                <el-input v-model="ruleForm.title"></el-input>
            </el-form-item>
            <el-form-item label="路由路径" prop="path">
                <el-input v-model="ruleForm.path"></el-input>
            </el-form-item>
            <el-form-item label="页面文件路径" prop="component">
                <el-input v-model="ruleForm.component" placeholder="顶级菜单请填写 layout"></el-input>
            </el-form-item>
            <el-form-item label="是否显示在侧边栏">
                <el-radio v-model="ruleForm.hidden" label="0">显示</el-radio>
                <el-radio v-model="ruleForm.hidden" label="1">不显示</el-radio>
            </el-form-item>
            <!-- <el-form-item label="平台类型">
                <el-radio v-model="ruleForm.type" label="PLATFORM">平台管理端</el-radio>
                <el-radio v-model="ruleForm.type" label="MERCHANT">商家端</el-radio>
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" :loading="loading" @click="sub('ruleForm')">确定</el-button>
                <el-button @click="onCancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { apiMenuDetail, apiMenuEdit } from "@/api/admin.js";
export default {
    name: "menu_index_edit",
    props: {
        editId: {
            type: Number,
            required: true
        }
    },
    data() {
        return {
            ruleForm: {
                sort: "",
                title: "",
                path: "",
                // redirect: "",
                component: "",
                hidden: "",
                type: "PLATFORM", // PLATFORM-平台菜单 MERCHANT-商家端菜单
            },
            rules: {
                sort: [{ required: true, message: "请输入排序", trigger: "change" }],
                title: [{ required: true, message: "请输入菜单名称", trigger: "change" }],
                path: [{ required: true, message: "请输入路径", trigger: "change" }],
                component: [{ required: true, message: "请输入页面路径", trigger: "change" }],
            },
            loading: false,
        }
    },
    created() {
        this.getData();
    },
    watch: {
        editId() {
            this.getData();
        }
    },
    methods: {
        getData() {
            apiMenuDetail({ id: this.editId }).then(res => {
                const { data } = res
                data.hidden = data.hidden.toString()
                this.ruleForm = res.data;
            });
        },
        // 确定
        sub(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loading = true;
                    apiMenuEdit(this.ruleForm).then((res) => {
                        this.$message({ message: "操作成功", type: "success" });
                        this.loading = false;
                        this.$emit("onSubmit");
                    }).catch((err) => {
                        this.loading = false;
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        // 取消
        onCancel() {
            this.loading = false;
            this.$emit("onCancel");
        },
        // 重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
}
</script>

<style></style>